package com.cn.ccp.action.classschedule;

import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.ClassScheduleDetailBean;
import com.cn.ccp.bean.SpecialtyBean;
import com.cn.ccp.bean.UserBean;
import com.cn.ccp.util.CCPContants;
import com.cn.ccp.util.Contants;

/**
 * 课程表action
 * @author renyongchengO
 *O
 */
public class ClassScheduleDetailEditAction extends BaseAction{
	private Logger log = Logger.getLogger(ClassScheduleDetailEditAction.class);
	private ClassScheduleDetailBean bean;
	private SelectItem[] specialtyItem ;//专业下拉框值
	private SelectItem[] teacherItem ;//老师下拉框值
	private boolean initData;
	private boolean showDeleteBtn;
	private boolean disableTeachItem;
	    	 
    public boolean isInitData() {
    	Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		Object showTeach = varMap.get("showTeacherSchedule");
		boolean showTeacherSchedule = false;
		if(showTeach != null && "true".equalsIgnoreCase(showTeach.toString()))
			showTeacherSchedule = true; 
		log.info("-----------id = "+id); 
		if(id != null){
			bean = (ClassScheduleDetailBean)getClassScheduleDetailService().findClassScheduleById(Integer.parseInt(id));
			showDeleteBtn = true;
			if(bean.getTeacherId() != null  && !bean.getTeacherId().equals("")){
				try {
//					specialtyId = getTeacherService().getSpecialtyIdById(bean.getTeacherId());
					List<UserBean> splist = getUserService().getAllTeachers(CCPContants.getUserBean().getFacultyId() );
					teacherItem = new SelectItem[splist.size()];
					for (int i = 0; i < splist.size(); i++) 
						teacherItem[i] = new SelectItem(String.valueOf(splist.get(i).getId()),splist.get(i).getUserName());
				} catch (Exception e) {}
			}
		}
		if(bean == null)
			bean = new ClassScheduleDetailBean();
		if(showTeacherSchedule){
			bean.setTeacherId(CCPContants.getCurrUserId());
			disableTeachItem = true;
		}
		return initData;
	}

	public void setInitData(boolean initData) {
		this.initData = initData;
	}

	public boolean isShowDeleteBtn() {
		return showDeleteBtn;
	}

	public void setShowDeleteBtn(boolean showDeleteBtn) {
		this.showDeleteBtn = showDeleteBtn;
	}

	public String update(){
    	if(bean.getTeacherId() == null  || bean.getTeacherId().equals("-1")){
    		showMsg("teacherId", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
    	if(bean.getSubjects() == null  || "".equalsIgnoreCase(bean.getSubjects().trim()) ){
    		showMsg("subjects", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
    	log.info(bean);
    	getClassScheduleDetailService().update(bean);
    	return MAIN;
    }
	
	public String delete(){
    	if(bean.getTeacherId() == null  || bean.getTeacherId().equals("-1")){
    		showMsg("teacherId", "javax.faces.component.UIInput.REQUIRED");
    		return "";
    	}
    	log.info(bean);
    	bean.setSubjects(Contants.SCHEDULE_CLASS_SUBJECT_INIT);
    	bean.setTeacherId("0");
    	bean.setClassAddress("");
    	getClassScheduleDetailService().update(bean);
    	return MAIN;
    }
	
	
    
    /**
	 * 选择的专业变化时
	 * 对应的老师下拉框需 要变化
	 * @param e
	 */
	public void changeSpecialtyForTeacher(){
//		specialtyId = (String) e.getNewValue() ;
		teacherItem = null ;
//		if(!specialtyId.equals("-1")){
//			List<UserBean> splist = getUserService().getAllTeachers(specialtyId);
//			teacherItem = new SelectItem[splist.size()];
//			for (int i = 0; i < splist.size(); i++) 
//				teacherItem[i] = new SelectItem(String.valueOf(splist.get(i).getId()),splist.get(i).getUserName());
//		}
	}
    

	public ClassScheduleDetailBean getBean() {
		return bean;
	}

	public void setBean(ClassScheduleDetailBean bean) {
		this.bean = bean;
	}

	public SelectItem[] getSpecialtyItem() {
		if(specialtyItem == null){
			List<SpecialtyBean> specialtyList = getSpecialtyService().pageQuerySpecialty(0, 100);
			if(specialtyList == null){
				specialtyItem = new SelectItem[0];
				specialtyItem[0] = new SelectItem("-1" , "No choose");
			}else{
				specialtyItem = new SelectItem[specialtyList.size()];
				for(int i = 0 ; i < specialtyList.size() ; i++){
					SpecialtyBean bean = specialtyList.get(i);
					specialtyItem[i] = new SelectItem(String.valueOf(bean.getId()) , bean.getSpecialtyname());
				}
			}
		}
		return specialtyItem;
	}

	public void setSpecialtyItem(SelectItem[] specialtyItem) {
		this.specialtyItem = specialtyItem;
	}

	public SelectItem[] getTeacherItem() {
		if(teacherItem == null)
			teacherItem = new SelectItem[0];
		return teacherItem;
	}

	public void setTeacherItem(SelectItem[] teacherItem) {
		this.teacherItem = teacherItem;
	}

	public boolean isDisableTeachItem() {
		return disableTeachItem;
	}

	public void setDisableTeachItem(boolean disableTeachItem) {
		this.disableTeachItem = disableTeachItem;
	}
 
}
